package com.sf.service.impl;

import com.sf.manager.CollectInfoManager;
import com.sf.manager.ProMarketBaseManager;
import com.sf.model.CollectInfo;
import com.sf.model.CollectStatus;
import com.sf.model.ProMarketBase;
import com.sf.model.ProMarketBaseExample;
import com.sf.util.MarathonConst;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class ProMarketJob{
    private static final Logger logger = LoggerFactory.getLogger(ProMarketJob.class);
    @Autowired
    private CollectInfoManager collectInfoManager;
    @Autowired
    private ProMarketBaseManager proMarketBaseManager;

    public void doBiz() {


        //查询所有的专业市场信息
        List<ProMarketBase> proMarketBases = proMarketBaseManager.selectByExample(new ProMarketBaseExample());
        if (CollectionUtils.isEmpty(proMarketBases)) {
            logger.error("proMarketBases is empty");
            return;
        }
        //循环处理所有的专业市场集货团信息
        for (ProMarketBase proMarketBase : proMarketBases) {
            processMarketBase(proMarketBase);
        }

    }

    private void processMarketBase(ProMarketBase proMarketBase) {
        //判断是否有未完成的集货团
        CollectInfo collectInfo = collectInfoManager.queryUnFinishedCollectInfo(MarathonConst.COLLECTION_TYPE);
        if(collectInfo == null)
        {
            return ;
        }
        //判断是否到截止时间,如果到了插入新几率
        if (collectInfo.getEndTm().before(new Date())) {
            //更新集货团状态为取消
            collectInfo.setStatus(CollectStatus.CANCELED.getCode());
            collectInfoManager.updateByIdAndStatus(collectInfo, CollectStatus.CREATED.getCode(), CollectStatus.CANCELED.getCode());
            collectInfo.setId(null);
            collectInfo.setUserdef1(String.valueOf(Integer.valueOf(collectInfo.getUserdef1())+1));
            collectInfoManager.insert(collectInfo, proMarketBase);

        }
    }


}
